#include "structtable.h"
#include <string.h>
#include <stdio.h>

#define STRUCT_TABLE_MAX 100
struct struct_entry structtable[STRUCT_TABLE_MAX];
int structtable_lastentry = -1;

int struct_insert(struct struct_entry *e) {

	int index = struct_lookup(e->name);
	if (index >= 0) {
		return index;
	}

	if (structtable_lastentry +1 >= STRUCT_TABLE_MAX) {
		return -1;
	}

	structtable_lastentry++;
	memcpy(&structtable[structtable_lastentry], e, sizeof(struct struct_entry));
	return structtable_lastentry;
}

int struct_lookup(const char *name) {
	for (int i = 0; i <= structtable_lastentry; i++) {
		if (strcmp(structtable[i].name, name) == 0) {
			return i;
		}
	}
	return -1;
}

struct struct_entry *struct_entryat(int index) {
	if (index >= 0 && index <= structtable_lastentry) {
		return structtable +index;
	}
}

void struct_print() {
	for (int i = 0; i <= structtable_lastentry; i++) {
		printf("struct: %s\n", structtable[i].name);
	}
}
